package com.tech.arno.spicysdk.videosdk.widget;

/*
*   @package:     com.tech.arno.spicysdk.videosdk
*   @fileName:    CustomVideoView
*   @author:    ArnoFrost
*   @date:  2017/12/13 0013 15:22
*   @desc:     负责视频播放,暂停以及各类事件的触发
*/

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tech.arno.spicysdk.R;
import com.tech.arno.spicysdk.videosdk.constant.SDKConstant;
import com.tech.arno.spicysdk.videosdk.core.AdContextParameters;
import com.util.LogUtil;
import com.util.Utils;

public class CustomVideoView extends RelativeLayout implements View.OnClickListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener,
        MediaPlayer.OnInfoListener, TextureView.SurfaceTextureListener {


    /**
     * Constant
     * 播放器生命周期状态
     */
    private static final String TAG = "MraidVideoView";
    private static final int TIME_MSG = 0x01; //handleMessage时间
    private static final int TIME_INTERVAL = 1000;//时间间隔
    private static final int STATE_ERROR = -1;//
    private static final int STATE_IDLE = 0;// 空闲状态
    private static final int STATE_PLAYING = 1;//播放状态
    private static final int STATE_PAUSING = 2;//暂停状态

    private static final int LOAD_TOTAL_COUNT = 3;//重试次数

    /**
     * UI
     */
    private ViewGroup mParentContainer; //父容器
    private RelativeLayout mPlayerView; //当前布局
    private TextureView mVideoView; //显示数据
    private Button mMiniPlayBtn;//最小化
    private ImageView mFullBtn;//全屏
    private ImageView mLoadingBar;//加载进度条
    private ImageView mFrameView;//
    private AudioManager audioManager;//音量控制器
    private Surface videoSurface;//最终真正显示的

    /**
     * Data
     */
    private String mUrl; //要加载的视频地址
    private String mFrameURI;
    private boolean isMute;//是否静音
    private int mScreenWidth, mDestationHeight;//屏幕宽高 高度为16:9


    /**
     * Status状态保护
     */
    private boolean canPlay = true;
    private boolean mIsRealPause;// 是否真正暂停
    private boolean mIsComplete;// 是否真正播放完毕
    private int mCurrentCount;//
    private int playerState = STATE_IDLE;//标记当前播放器处于什么状态

    private MediaPlayer mediaPlayer;

    private ADVideoPlayerListener listener; //事件监听回调
    private ScreenEventReceiver mScreenReceiver;//监听屏幕是否锁屏
    //每隔 TIME_INTERVAL 发送一个timeMessage事件 来执行一次handleMessage
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TIME_MSG:
                    if (isPlaying()) {
                        //还可以在这里更新progressbar
                        //LogUtils.i(TAG, "TIME_MSG");
                        listener.onBufferUpdate(getCurrentPosition());//播放中检测
                        sendEmptyMessageDelayed(TIME_MSG, TIME_INTERVAL);
                    }
                    break;
            }
        }
    };

    //private ImageLoaderUtils mImageLoader;

    private ADFrameImageLoadListener mFrameLoadListener;

    public CustomVideoView(Context context, ViewGroup parentContainer) {
        super(context);
        mParentContainer = parentContainer;
        audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        initData();
        initView();
        registerBroadcastReceiver();
    }

    private void initData() {
        //获取屏幕宽高
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
        }
        mScreenWidth = dm.widthPixels;//屏幕宽
        mDestationHeight = (int) (mScreenWidth * SDKConstant.VIDEO_HEIGHT_PERCENT);//屏幕高度为16:9
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(this.getContext());
        mPlayerView = (RelativeLayout) inflater.inflate(R.layout.xadsdk_video_player, this);
        mVideoView = (TextureView) mPlayerView.findViewById(R.id.xadsdk_player_video_textureView);
        mVideoView.setOnClickListener(this);
        mVideoView.setKeepScreenOn(true);
        mVideoView.setSurfaceTextureListener(this);
        initSmallLayoutMode(); //init the small mode
    }

    // 小模式状态
    private void initSmallLayoutMode() {
        LayoutParams params = new LayoutParams(mScreenWidth, mDestationHeight);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mPlayerView.setLayoutParams(params);

        mMiniPlayBtn = (Button) mPlayerView.findViewById(R.id.xadsdk_small_play_btn);
        mFullBtn = (ImageView) mPlayerView.findViewById(R.id.xadsdk_to_full_view);
        mLoadingBar = (ImageView) mPlayerView.findViewById(R.id.loading_bar);
        mFrameView = (ImageView) mPlayerView.findViewById(R.id.framing_view);
        mMiniPlayBtn.setOnClickListener(this);
        mFullBtn.setOnClickListener(this);
    }

    private void registerBroadcastReceiver() {
        if (mScreenReceiver == null) {
            mScreenReceiver = new ScreenEventReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_USER_PRESENT);
            getContext().registerReceiver(mScreenReceiver, filter);
        }
    }

    private void unRegisterBroadcastReceiver() {
        if (mScreenReceiver != null) {
            getContext().unregisterReceiver(mScreenReceiver);
        }
    }

    public void isShowFullBtn(boolean isShow) {
        mFullBtn.setImageResource(isShow ? R.drawable.xadsdk_ad_mini : R.drawable.xadsdk_ad_mini_null);
        mFullBtn.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public boolean isPauseBtnClicked() {
        return mIsRealPause;
    }

    public boolean isComplete() {
        return mIsComplete;
    }


    /**
     * @param changedView
     * @param visibility
     * @name onVisibilityChanged
     * @function View在屏幕可见状态改变时回调函数
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:00
     */
    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        LogUtil.i(TAG, "onVisibilityChanged" + visibility);
        super.onVisibilityChanged(changedView, visibility);
        //处理只有一个播放的播放器并且可见才去播放,否则暂停
        if (visibility == VISIBLE && playerState == STATE_PAUSING) {
            if (isPauseBtnClicked() || isComplete()) {
                //标明播放器进入了真正的暂停状态
                pause();
            } else {
                //去通过条件判断是否可以播放
                decideCanPlay();
            }
        } else {
            pause();
        }
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return super.onInterceptTouchEvent(ev);
//    }

    @Override
    protected void onDetachedFromWindow() {
        LogUtil.i(TAG, "onDetachedFromWindow");
        super.onDetachedFromWindow();
    }

    //触摸事件处理 防止与父容器冲突
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return true;
    }

    /**
     * true is no voice
     *
     * @param mute
     */
    public void mute(boolean mute) {
        LogUtil.d(TAG, "mute");
        isMute = mute;
        if (mediaPlayer != null && this.audioManager != null) {
            float volume = isMute ? 0.0f : 1.0f;
            mediaPlayer.setVolume(volume, volume);
        }
    }

    /**
     * @return boolean
     * @name isPlaying
     * @function 判断是否是播放状态
     * @author ArnoFrost
     * @time 2017/12/13 0013  17:12
     */
    public boolean isPlaying() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            return true;
        }
        return false;
    }

    public boolean isFrameHidden() {
        return mFrameView.getVisibility() == View.VISIBLE ? false : true;
    }

    public void
    setIsComplete(boolean isComplete) {
        mIsComplete = isComplete;
    }

    public void setIsRealPaused(boolean isClicked) {
        this.mIsRealPause = isClicked;
    }

    //点击事件处理
    @Override
    public void onClick(View v) {
        if (v == this.mMiniPlayBtn) {
            if (this.playerState == STATE_PAUSING) {
                if (Utils.getVisiblePercent(mParentContainer)
                        > SDKConstant.VIDEO_SCREEN_PERCENT) {
                    resume();
                    this.listener.onClickPlay();
                }
            } else {
                load();
            }
        } else if (v == this.mFullBtn) {
            this.listener.onClickFullScreenBtn();
        } else if (v == mVideoView) {
            this.listener.onClickVideo();
        }
    }


    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return true;
    }

    //=======================================
    //加载数据,MediaPlayer过程
    public void load() {
        //判断当前播放状态是否等于空
        if (this.playerState != STATE_IDLE) {
            return;
        }
        //通过
        LogUtil.d(TAG, "do play url = " + this.mUrl);
        showLoadingView();
        try {
            //设置当前状态为空闲
            setCurrentPlayState(STATE_IDLE);
            //去检查播放器是否为空,为空则创建,完成播放器初始化
            checkMediaPlayer();
            mute(true);
            //设置视频播放URL
            mediaPlayer.setDataSource(this.mUrl);
            //开始异步加载
            mediaPlayer.prepareAsync();
        } catch (Exception e) {
            LogUtil.e(TAG, e.getMessage());
            stop(); //error以后重新调用stop加载
        }
    }

    /**
     * {@link CustomVideoView#LOAD_TOTAL_COUNT 循环次数 }
     *
     * @name
     * @function 播放器停止
     * @author ArnoFrost
     * @time 2017/12/13 0013  17:04
     */
    public void stop() {
        LogUtil.d(TAG, " do stop");
        //释放mediaPlayer的内容
        if (this.mediaPlayer != null) {
            this.mediaPlayer.reset();
            this.mediaPlayer.setOnSeekCompleteListener(null);
            this.mediaPlayer.stop();
            this.mediaPlayer.release();
            this.mediaPlayer = null;
        }
        //移除Message
        mHandler.removeCallbacksAndMessages(null);
        //设置为空闲状态
        setCurrentPlayState(STATE_IDLE);
        //满足重新加载的条件 去重试LOAD_TOTAL_COUNT的次数
        if (mCurrentCount < LOAD_TOTAL_COUNT) {
            mCurrentCount += 1;
            //重新加载
            load();
        } else {
            showPauseView(false); //显示暂停状态
        }
    }

    /**
     * @param mp
     * @return
     * @name onPrepared
     * @function 播放器处于就绪状态
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:03
     */
    @Override
    public void onPrepared(MediaPlayer mp) {
        LogUtil.i(TAG, "onPrepared");
        showPlayView();
        mediaPlayer = mp;
        if (mediaPlayer != null) {
            mediaPlayer.setOnBufferingUpdateListener(this);
            mCurrentCount = 0;
            if (listener != null) {
                //通知视频加载成功
                listener.onAdVideoLoadSuccess();
            }
            //满足自动播放条件，则直接播放
            if (Utils.canAutoPlay(getContext(),
                    AdContextParameters.getCurrentSetting()) &&
                    Utils.getVisiblePercent(mParentContainer) > SDKConstant.VIDEO_SCREEN_PERCENT) {
                setCurrentPlayState(STATE_PAUSING);
                resume();
            } else {
                setCurrentPlayState(STATE_PLAYING);
                pause();
            }
        }
    }

    public void resume() {
        //判断当前是否是播放状态
        if (this.playerState != STATE_PAUSING) {
            return;
        }
        LogUtil.d(TAG, "do resume");
        if (!isPlaying()) {
            //设置为播放中的状态值
            entryResumeState();
            mediaPlayer.setOnSeekCompleteListener(null);
            mediaPlayer.start();
            //播放开始 发送handle消息 (只有播放时才发送消息)
            mHandler.sendEmptyMessage(TIME_MSG);
            //显示
            showPauseView(true);
        } else {
            showPauseView(false);
        }
    }

    public void pause() {
        //判断状态
        if (this.playerState != STATE_PLAYING) {
            return;
        }
        //通过
        LogUtil.d(TAG, "do pause");
        setCurrentPlayState(STATE_PAUSING);
        if (isPlaying()) {
            mediaPlayer.pause();
            if (!this.canPlay) {
                this.mediaPlayer.seekTo(0);
            }
        }
        this.showPauseView(false);
        mHandler.removeCallbacksAndMessages(null);
    }

    /**
     * @param mp MediaPlayer
     * @name onCompletion
     * @function 在视频播放完以后回调 OnCompletionListener回调
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:01
     */
    @Override
    public void onCompletion(MediaPlayer mp) {
        if (listener != null) {
            listener.onAdVideoLoadComplete();
        }
        playBack();
        //改变状态为播放完成
        setIsComplete(true);
        //改变为视频真正暂停状态
        setIsRealPaused(true);
    }

    /**
     * @param mp    MediaPlayer
     * @param what
     * @param extra
     * @return boolean
     * @name onError
     * @function 播放器产生异常时候通知
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:02
     */
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        LogUtil.e(TAG, "do error:" + what);
        this.playerState = STATE_ERROR;
        mediaPlayer = mp;
        if (mediaPlayer != null) {
            //重置
            mediaPlayer.reset();
        }
        if (mCurrentCount >= LOAD_TOTAL_COUNT) {
            showPauseView(false);
            if (this.listener != null) {
                listener.onAdVideoLoadFailed();
            }
        }
        this.stop();//去重新load
        return true;
    }
    //load完整过程结束
    //=======================================

    /**
     * @param mp      MediaPlayer
     * @param percent 百分比
     * @return
     * @name onBufferingUpdate
     * @function 缓存回调
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:05
     */
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }

    /**
     * @param surface
     * @param width
     * @param height
     * @return
     * @name onSurfaceTextureAvailable
     * @function Texture处于完成状态
     * @author ArnoFrost
     * @time 2017/12/13 0013  16:05
     */
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        LogUtil.i(TAG, "onSurfaceTextureAvailable");
        //不应写在构造方法,一定要在TextureView可用后
        videoSurface = new Surface(surface);
        //检查是否可用
        checkMediaPlayer();
        mediaPlayer.setSurface(videoSurface);
        load();
    }

    private synchronized void checkMediaPlayer() {
        if (mediaPlayer == null) {
            mediaPlayer = createMediaPlayer(); //每次都重新创建一个新的播放器
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        LogUtil.i(TAG, "onSurfaceTextureSizeChanged");
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    public void setDataSource(String url) {
        this.mUrl = url;
    }

    public void setFrameURI(String url) {
        mFrameURI = url;
    }

    /**
     * @param
     * @return
     * @name showPauseView
     * @function 显示播放界面
     * @author ArnoFrost
     * @time 2017/12/13 0013  17:14
     */
    private void showPauseView(boolean show) {
        mFullBtn.setVisibility(show ? View.VISIBLE : View.GONE);
        mMiniPlayBtn.setVisibility(show ? View.GONE : View.VISIBLE);
        mLoadingBar.clearAnimation();
        mLoadingBar.setVisibility(View.GONE);
        if (!show) {
            mFrameView.setVisibility(View.VISIBLE);
            loadFrameImage();
        } else {
            mFrameView.setVisibility(View.GONE);
        }
    }

    private void showLoadingView() {
        mFullBtn.setVisibility(View.GONE);
        mLoadingBar.setVisibility(View.VISIBLE);
        AnimationDrawable anim = (AnimationDrawable) mLoadingBar.getBackground();
        anim.start();
        mMiniPlayBtn.setVisibility(View.GONE);
        mFrameView.setVisibility(View.GONE);
        loadFrameImage();
    }

    private void showPlayView() {
        mLoadingBar.clearAnimation();
        mLoadingBar.setVisibility(View.GONE);
        mMiniPlayBtn.setVisibility(View.GONE);
        mFrameView.setVisibility(View.GONE);
    }

    /**
     * 异步加载定帧图
     */
    private void loadFrameImage() {
        if (mFrameLoadListener != null) {
            mFrameLoadListener.onStartFrameLoad(mFrameURI, new ImageLoaderListener() {
                @Override
                public void onLoadingComplete(Bitmap loadedImage) {
                    if (loadedImage != null) {
                        mFrameView.setScaleType(ImageView.ScaleType.FIT_XY);
                        mFrameView.setImageBitmap(loadedImage);
                    } else {
                        mFrameView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        mFrameView.setImageResource(R.drawable.xadsdk_img_error);
                    }
                }
            });
        }
    }


    //全屏不显示暂停状态,后续可以整合，不必单独出一个方法
    public void pauseForFullScreen() {
        if (playerState != STATE_PLAYING) {
            return;
        }
        LogUtil.d(TAG, "do full pause");
        setCurrentPlayState(STATE_PAUSING);
        if (isPlaying()) {
            mediaPlayer.pause();
            if (!this.canPlay) {
                mediaPlayer.seekTo(0);
            }
        }
        mHandler.removeCallbacksAndMessages(null);
    }

    public int getCurrentPosition() {
        if (this.mediaPlayer != null) {
            return mediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    public int getDuration() {
        if (mediaPlayer != null) {
            return mediaPlayer.getDuration();
        }
        return 0;
    }

    //跳到指定点播放视频
    public void seekAndResume(int position) {
        if (mediaPlayer != null) {
            showPauseView(true);
            entryResumeState();
            mediaPlayer.seekTo(position);
            mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    LogUtil.d(TAG, "do seek and resume");
                    mediaPlayer.start();
                    mHandler.sendEmptyMessage(TIME_MSG);
                }
            });
        }
    }

    //跳到指定点暂停视频
    public void seekAndPause(int position) {
        if (this.playerState != STATE_PLAYING) {
            return;
        }
        showPauseView(false);
        setCurrentPlayState(STATE_PAUSING);
        if (isPlaying()) {
            mediaPlayer.seekTo(position);
            mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    LogUtil.d(TAG, "do seek and pause");
                    mediaPlayer.pause();
                    mHandler.removeCallbacksAndMessages(null);
                }
            });
        }
    }


    /**
     * 进入播放状态时的状态更新
     */
    private void entryResumeState() {
        canPlay = true;
        setCurrentPlayState(STATE_PLAYING);
        setIsRealPaused(false);
        setIsComplete(false);
    }

    private void setCurrentPlayState(int state) {
        playerState = state;
    }

    //播放完成后回到初始状态
    public void playBack() {
        LogUtil.d(TAG, " do playBack");
        //设置状态为暂停
        setCurrentPlayState(STATE_PAUSING);
        mHandler.removeCallbacksAndMessages(null);
        if (mediaPlayer != null) {
            //不为空时 重置到第0秒并暂停
            mediaPlayer.setOnSeekCompleteListener(null);
            mediaPlayer.seekTo(0);
            mediaPlayer.pause();
        }
        this.showPauseView(false);
    }


    public void destroy() {
        LogUtil.d(TAG, " do destroy");
        if (this.mediaPlayer != null) {
            this.mediaPlayer.setOnSeekCompleteListener(null);
            this.mediaPlayer.stop();
            this.mediaPlayer.release();
            this.mediaPlayer = null;
        }
        setCurrentPlayState(STATE_IDLE);
        mCurrentCount = 0;
        setIsComplete(false);
        setIsRealPaused(false);
        unRegisterBroadcastReceiver();
        mHandler.removeCallbacksAndMessages(null); //release all message and runnable
        showPauseView(false); //除了播放和loading外其余任何状态都显示pause
    }


    public void setListener(ADVideoPlayerListener listener) {
        this.listener = listener;
    }

    public void setFrameLoadListener(ADFrameImageLoadListener frameLoadListener) {
        this.mFrameLoadListener = frameLoadListener;
    }

    private MediaPlayer createMediaPlayer() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.reset();
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnInfoListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        if (videoSurface != null && videoSurface.isValid()) {
            mediaPlayer.setSurface(videoSurface);
        } else {
            stop();
        }
        return mediaPlayer;
    }

    private void decideCanPlay() {
        if (Utils.getVisiblePercent(mParentContainer) > SDKConstant.VIDEO_SCREEN_PERCENT)
            //来回切换页面时，只有 >50,且满足自动播放条件才自动播放
            resume();
        else
            pause();
    }

    /**
     * 监听锁屏事件的广播接收器
     */
    private class ScreenEventReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //主动锁屏时 pause, 主动解锁屏幕时，resume
            switch (intent.getAction()) {
                case Intent.ACTION_USER_PRESENT:
                    if (playerState == STATE_PAUSING) {
                        if (mIsRealPause) {
                            //手动点的暂停，回来后还暂停
                            pause();
                        } else {
                            decideCanPlay();
                        }
                    }
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    if (playerState == STATE_PLAYING) {
                        pause();
                    }
                    break;
            }
        }
    }

    /**
     * 供slot层来实现具体点击逻辑,具体逻辑还会变，
     * 如果对UI的点击没有具体监测的话可以不回调
     */
    public interface ADVideoPlayerListener {
        //视频播放器播放到了几秒
        public void onBufferUpdate(int time);

        //全屏跳转监听
        public void onClickFullScreenBtn();

        //点击视频
        public void onClickVideo();

        //返回
        public void onClickBackBtn();

        //播放
        public void onClickPlay();

        //加载成功
        public void onAdVideoLoadSuccess();

        //加载失败
        public void onAdVideoLoadFailed();

        //加载完毕
        public void onAdVideoLoadComplete();
    }

    public interface ADFrameImageLoadListener {

        void onStartFrameLoad(String url, ImageLoaderListener listener);
    }

    /**
     * 供slot层来实现具体点击逻辑,具体逻辑还会变，
     * 如果对UI的点击没有具体监测的话可以不回调
     */
    public interface VideoPlayerListener {
        //视频播放器播放到了几秒
        public void onBufferUpdate(int time);

        //全屏跳转监听
        public void onClickFullScreenBtn();

        //点击视频
        public void onClickVideo();

        //返回
        public void onClickBackBtn();

        //播放
        public void onClickPlay();

        //加载成功
        public void onAdVideoLoadSuccess();

        //加载失败
        public void onAdVideoLoadFailed();

        //加载完毕
        public void onAdVideoLoadComplete();
    }

    public interface VideoFrameImageLoadListener {

        void onStartFrameLoad(String url, ImageLoaderListener listener);
    }

    public interface ImageLoaderListener {
        /**
         * 如果图片下载不成功，传null
         *
         * @param loadedImage
         */
        void onLoadingComplete(Bitmap loadedImage);
    }

    private void setVideoBuffer() {
        if (Build.VERSION.SDK_INT >= 15) {
            SurfaceTexture texture = mVideoView.getSurfaceTexture();
            if (texture != null) {
                texture.setDefaultBufferSize(mediaPlayer.getVideoWidth(),
                        mediaPlayer.getVideoHeight());
            }
        }
    }
}
